﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmos.Hardware.BlockDevice;
using GruntyOS.HAL;
namespace CosmosKernel4
{
    class Init
    {
        public void DoInit()
        {
            Console.WriteLine("RAM availability: " + Cosmos.Core.CPU.GetAmountOfRAM().ToString() + "MB");
            #region ArrayReverser Unit Test
            /*Byte[] testdata = { 0xB1, 0x9B, 0x00, 0xB5 };
            testdata = CosmosKernel4.Global.ArrayReverser.ReverseArray(testdata);
            if (BitConverter.ToUInt32(testdata, 0)!= 0xB1B900B5)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ArrayReverser unit test FAILED!");

            }
            */
            #endregion

            #region CPUIDJUNK
            String VendorStr = Cosmos.Kernel.CPU.CPUVendor;
            if (Cosmos.Kernel.CPU.CPUIDSupport != true)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("KERNEL WARNING:CPUID NOT SUPPORTED!");
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            if (VendorStr != String.Empty)
            {
                Console.WriteLine("CPU Vendor String:" + VendorStr);
            }
#endregion
            
            #region FileSystemInit
            Console.WriteLine("Initialising FileSystem");
            if (BlockDevice.Devices.Count > 0)
            {
                for (int i = 0; i < BlockDevice.Devices.Count; i++)
                {
                    BlockDevice Device = BlockDevice.Devices[i];
                    if (Device is Partition)
                    {
                        

                        GLNFS fd = new GLNFS((Partition)Device);
                        Console.Write(Device.BlockSize);
                        Console.Write(":");
                        Console.WriteLine(Device.BlockCount);
                        if (GLNFS.isGFS((Partition)Device))
                        {
                            
                            Console.WriteLine("GLNFS FOUND!");
                            Console.WriteLine("Checking for Plasmid files");


                            bool plas_found = false;
                            string[] dirs = fd.ListDirectories("/");
                            for (int m = 0; m < dirs.Length; m++)
                            {
                                Console.WriteLine(dirs[m]);
                                if (dirs[m] == "plasmid")
                                {
                                    GruntyOS.CurrentUser.Username = "SYSTEM";
                                    
                                    GruntyOS.CurrentUser.Privilages = 0;
                                    
                                    plas_found = true;
                                    CosmosKernel4.Global.Config.compname = CosmosKernel4.Global.StringManip.GetString(fd.readFile("/plasmid/compname"));
                                    GruntyOS.HAL.FileSystem.Root = fd;
                                    Console.WriteLine("PLASMID FILESYSTEM FOUND; SETTING AS ROOT");
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine("BOOTSTRAP SEQUENCE DONE");
                                    GruntyOS.HAL.FileSystem.Root = fd;
                                    CosmosKernel4.UI.Login.Login
                                    lgin = new CosmosKernel4.UI.Login.Login();
                                    lgin.do_login();
                                    goto infiniloop;
                                }
                            }
                            if (plas_found == false)
                            {
                                Console.WriteLine("Plasmid files not found. Press any key to enter install");
                                Console.ReadKey();
                                new CosmosKernel4.Install.plasmidInstall().Install();
                                Console.WriteLine("Press any key to reboot....");
                                int ignore = Console.Read();
                                new CosmosKernel4.PowerManagement.PowerMgr().reboot();
                            }

                        }
                        else
                        {
                            Console.WriteLine("Filesystem not present, formatting...");
                            fd.Format("PLASMID");
                            fd.Label = 'x';

                            Console.ReadKey();
                            Console.WriteLine("Your computer will now reboot.");
                            CosmosKernel4.PowerManagement.PowerMgr pmg = new CosmosKernel4.PowerManagement.PowerMgr();
                            pmg.reboot();
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Diskless machines suck, eh?");
            }
            #endregion
            
        infiniloop:
            goto infiniloop;

        }
    }
}
